package com.innovation.sys.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

public class MD5Utils {

	private static Logger logger = LoggerFactory.getLogger(MD5Utils.class);
	
	public static String strToMD5(String src) {
		StringBuffer sb = new StringBuffer();
        try {
            // 加密对象，指定加密方式
            MessageDigest md5 = MessageDigest.getInstance("md5");
            // 准备要加密的数据
            byte[] b = src.getBytes();
            // 加密
            byte[] digest = md5.digest(b);
            // 十六进制的字符
            char[] chars = new char[] { '0', '1', '2', '3', '4', '5',
                 '6', '7' , '8', '9', 'A', 'B', 'C', 'D', 'E','F' };
            // 处理成十六进制的字符串(通常)
            for (byte bb : digest) {
                sb.append(chars[(bb >> 4) & 15]);
                sb.append(chars[bb & 15]);
            }
        } catch (NoSuchAlgorithmException e) {
        	logger.info("error:{}" ,e);
        }
        return sb.toString();
		
	}

	public static String strToMD52(String src) {
		if (!StringUtils.isEmpty(src)) {
			try {
				MessageDigest md = MessageDigest.getInstance("MD5");
				md.update(src.getBytes());
				StringBuilder dist = new StringBuilder();
				for (byte b : md.digest()) {
					String s = Integer.toHexString(0xFF & b);
					if (s.length() == 1)
						dist.append("0");
					dist.append(s);
				}
				return dist.toString().toUpperCase();
			} catch (Exception e) {
				logger.info("error:{}" ,e);
			}
		}
		return "";
	}
	
	public static void main(String[] args) {
		System.out.println(strToMD5("111"));
	}
	

}
